package com.example.wochacha.mvpdemo.login.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.example.wochacha.mvpdemo.R;
import com.example.wochacha.mvpdemo.login.DaggerLoginComponent;
import com.example.wochacha.mvpdemo.login.LoginModule;
import com.example.wochacha.mvpdemo.login.presenter.ILoginPresenter;

import javax.inject.Inject;

/**
 * Created by josan on 2017/12/1.
 */

public class LoginActivity extends AppCompatActivity implements ILoginView, View.OnClickListener {
    private EditText editUser;
    private EditText editPass;
    private Button   btnLogin;
    private Button btnClear;
    /**
     * 表明ILoginPresenter是需要注入到LoginActivity到
     * 即LoginActivity依赖于ILoginPresenter
     * 需要注意到是，用@Inject时，不能用private修饰类到成员属性
     */
    @Inject
    ILoginPresenter loginPresenter;
    private ProgressBar progressBar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        //find view
        editUser = (EditText) this.findViewById(R.id.et_login_username);
        editPass = (EditText) this.findViewById(R.id.et_login_password);
        btnLogin = (Button) this.findViewById(R.id.btn_login_login);
        btnClear = (Button) this.findViewById(R.id.btn_login_clear);
        progressBar = (ProgressBar) this.findViewById(R.id.progress_login);
        //set listener
        btnLogin.setOnClickListener(this);
        btnClear.setOnClickListener(this);

        // 执行该方法后，Component就将@Inject注解的loginPresenter与其构造方法联系起来了
        // 通过new LoginModule(this)将ILoginView传入到LoginModule中
        // 在LoginModule中的providerLoginView方法妇女会这个ILoginView对象
        // 当去初始化LoginPersenter时，发现构造含有有个参数时，此时会在Module里查找提供这个依赖当方法
        // 将该View传递进去，这样就完成了Presenter里View的注入
        DaggerLoginComponent.builder()
                .loginModule(new LoginModule(this))
                .build()
                .inject(this);
        //loginPresenter = new LoginPresenterCompl(this);
        loginPresenter.setProgressBarVisiblity(View.INVISIBLE);
    }

    @Override
    public void onClearText() {
        editUser.setText("");
        editPass.setText("");
    }

    @Override
    public void onLoginResult(Boolean result, int code) {
        loginPresenter.setProgressBarVisiblity(View.INVISIBLE);
        btnLogin.setEnabled(true);
        btnClear.setEnabled(true);
        if (result) {
            Toast.makeText(this, "登陆成功", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "登陆失败", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onSetProgressBarVisibility(int visibility) {
        progressBar.setVisibility(visibility);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login_clear:
                loginPresenter.clear();
                break;

            case R.id.btn_login_login:
                loginPresenter.setProgressBarVisiblity(View.VISIBLE);
                btnClear.setEnabled(false);
                btnLogin.setEnabled(false);
                loginPresenter.doLogin(editUser.getText().toString().trim(),
                        editPass.getText().toString());
                break;
        }
    }
}
